from fastapi import APIRouter, HTTPException, Depends
from typing import List
from sqlalchemy.orm import Session
from schema import Meteorology_schema
from controller import Meteorology_controller
from database.db_init import get_db


router = APIRouter()


@router.post("/Meteorology", response_model=Meteorology_schema.Meteorology, tags=["Meteorology"])
async def create_Meteorology(Meteorology: Meteorology_schema.Meteorology, db: Session = Depends(get_db)):
    return Meteorology_controller.create_Meteorology(db, Meteorology)


@router.get("/Meteorology/{ID}", response_model=Meteorology_schema.Meteorology, tags=["Meteorology"])
async def get_Meteorology(ID: int, db: Session = Depends(get_db)):
    db_Meteorology = Meteorology_controller.get_Meteorology(db, ID)
    if db_Meteorology is None:
        raise HTTPException(status_code=404, detail="Meteorology not found")
    return db_Meteorology


@router.get("/Meteorology", response_model=List[Meteorology_schema.Meteorology], tags=["Meteorology"])
async def get_all_Meteorology(db: Session = Depends(get_db)):
    all_db_Meteorology = Meteorology_controller.get_all_Meteorology(db)
    if all_db_Meteorology is None:
        raise HTTPException(status_code=404, detail="0 Meteorology found, empty table")
    return all_db_Meteorology


@router.put("/Meteorology/{ID}", tags=["Meteorology"])
async def update_Meteorology(ID: int, field_name: str, field_value: str, db: Session = Depends(get_db)):
    try:
        db_Meteorology = Meteorology_controller.update_Meteorology(db, ID, field_name, field_value)
        if db_Meteorology is None:
            raise HTTPException(status_code=404, detail="Meteorology not found")
        return db_Meteorology
    except ValueError as error:
        raise HTTPException(status_code=400, detail=str(error))


@router.delete("/Meteorology/{ID}", tags=["Meteorology"])
async def delete_Meteorology(ID: int, db: Session = Depends(get_db)):
    is_deleted = Meteorology_controller.delete_Meteorology(db, ID)
    if is_deleted is None:
        raise HTTPException(status_code=404, detail=f'Sample {ID} does not exist in database')
    else:
        return {'message': f'Successfully delete sample {ID}'}
